package cn.change.org.controller;

import cn.change.basic.util.AjaxResult;
import cn.change.basic.util.PageList;
import cn.change.org.domain.Employee;
import cn.change.org.query.EmployeeQuery;
import cn.change.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16252
 * 三要素
 * 1：请求地址
 * 2：请求地址的参数
 * 3：返回值是什么
 */
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 查询单条数据
     *
     * @param id
     * @return
     * @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”,
     * response = “接口返回参数类型”, notes = “接口发布说明”；
     */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }

    /**
     * 增加或修改
     *
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult saveAndUp(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable ("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public List<Employee> loadAll() {
        return employeeService.queryAll();
    }

    /**
     * 高级查询和分页
     *
     * @param employeeQuery
     * @return
     */
    @PostMapping
    public PageList<Employee> loadPage(@RequestBody EmployeeQuery employeeQuery) {
    return employeeService.queryPage(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }

    }
}